using System;
using System.Collections;
using System.Collections.Generic;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using Fantasy.Helper;
using Fantasy.Network;
using Hotfix.Entity;
using UnityEngine;
using UnityEngine.UI;

public class Entry : MonoBehaviour
{
    public Button btnSendMsg;
    public Button btnCallTest;
    public Button btnDisconnect;
    public Button btnEvent;
    public Button btnEventAsync;

    public Button btnConnectRoute;
    public Button btnSendRoute;
    public Button btnSendCallConnect;

    public static Scene GameScene;

    private async void Start()
    {
        await StartAsync();
    }

    private async FTask StartAsync()
    {
        // 初始化框架
        Fantasy.Platform.Unity.Entry.Initialize(GetType().Assembly);

        // 创建用一个客户端的Scene没如果有个别同学不需要使用框架的Scene
        // 那就把Scene当网络接口使用。
        var scene = await Fantasy.Platform.Unity.Entry.CreateScene();
        GameScene = scene;
        Session session = null;
        session =
            scene.Connect("127.0.0.1:20000", NetworkProtocolType.KCP, () =>
                {
                    print("连接完成");
                    //加入心跳
                    session.AddComponent<SessionHeartbeatComponent>().Start(2000);
                },
                () => { print("连接失败"); }, () => { print("断开连接"); }, false, 5000);
        btnCallTest.onClick.AddListener(async () =>
        {
            C2G_WxyMsgRsp rsp = (C2G_WxyMsgRsp)await session.Call(new C2G_WxyMsgReq()
            {
                name = "wxy"
            });
            print(rsp.tag);
        });
        btnDisconnect.onClick.AddListener(async () => { session.Dispose(); });
        btnEvent.onClick.AddListener(() =>
        {
            GameScene.EventComponent.Publish(Fantasy.Entitas.Entity.Create<User>(GameScene, true, true));
        });
        btnEventAsync.onClick.AddListener(async () =>
        {
            User user = Entity.Create<User>(GameScene, true, true);
            await FTask.Wait(GameScene, 1000);
            await GameScene.EventComponent.PublishAsync(user);
        });
        btnSendMsg.onClick.AddListener(() =>
        {
            session.Send(new C2G_TestMsg());
        });
        //创建路由消息
        btnConnectRoute.onClick.AddListener(async () =>
        {
            G2C_ConnectChatResponse1 rsp = (G2C_ConnectChatResponse1)await session.Call(new C2G_ConnectChatRequest1());
            if (rsp.ErrorCode != 0)
            {
                Log.Error("创建路由消息出现错误");
                return;
            }
        });
        //发送中转消息
        btnSendRoute.onClick.AddListener(() =>
        {
            session.Send(new C2Chat_WxyHelloMsg()
            {
                msg = "我是用来测试自定义消息中转的协议"
            });
            Log.Debug("发送了一个自定义消息中转的协议");
        });
        //发送带返回值的中转消息
        btnSendCallConnect.onClick.AddListener(async () =>
        {
            Chat2C_WxyHelloRsp chatRsp = (Chat2C_WxyHelloRsp)await session.Call(new C2Chat_WxyHelloReq()
            {
                msg = "我想你了聊天服务器"
            });
            Log.Debug($"收到聊天服务器的Msg: {chatRsp.msg}");
        });
    }
}